// pages/followList/followList.js
import api from "../../config/api";
import { shareOptions } from "../../utils/access";
import request from "../../utils/request";
import { getStorage } from "../../utils/utils"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFans:false,
    showNodata:false,
    userId:'',
    pages:0,
    page:1,
    showFollowBtn:true,
    list: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {userId,isFans} = options
    let title = ''
    let myUserId = getStorage('userId')
    console.log('myUserId',myUserId);

    if(isFans){
      title = myUserId==userId?'我的粉丝':'他的粉丝'
      this.getFansList(userId)
    }else{ 
      title = myUserId==userId?'我的关注':'他的关注'
      this.getFollowList(userId)
    }
    this.setData({
      showFollowBtn:myUserId==userId,
      isFans,
      userId,
      title,
    })
  },
  //获取关注列表
  getFollowList: async function(userId){
    let {list,page} = this.data
    const res = await request.get(api.getFollowList,{userId,page})
    if(res.httpStatus==200){
      console.log('关注',res);
      let data = res.data

      // data.records.map((item)=>{
      //   if(item.nickName.length>6){
      //     let arr = Array.from(item.nickName)
      //     item.nickName = arr[0]+arr[1]+arr[2]+arr[3]+arr[4]+arr[5]+'...'
      //     console.log('item.nickName',item.nickName);
      //   }
      // })
      console.log('data.records',data.records);

      this.setData({
        list:(list || []).concat(data.records),
        pages:data.pages
      })
    }
  },
  //获取粉丝列表
  getFansList: async function(userId){
    let {list,page} = this.data
    const res = await request.get(api.getFansList,{userId,page})
    if(res.httpStatus==200){
      console.log('粉丝',res);
      let data = res.data

      // data.records.map((item)=>{
      //   if(item.nickName.length>6){
      //     let arr = Array.from(item.nickName)
      //     item.nickName = arr[0]+arr[1]+arr[2]+arr[3]+arr[4]+arr[5]+'...'
      //     console.log('item.nickName',item.nickName);
      //   }
      // })
      console.log('data.records',data.records);
      this.setData({
        list:(list || []).concat(data.records),
        pages:data.pages
      })
    }
  },
  //点击关注按钮
  clickBtn: async function(e){
    console.log('e',e);
    let that = this
    let {index,item} = e.currentTarget.dataset
    let {userId} = item
    this.setData({
      selectItem:{
        userId,index
      }
    })
    if(item.follow){
      this.followModal.show()
    }else{
      that.handleFollow(userId,index)
    }
  },
  // 取关确认
  followConfirm() {
    let {userId,index} = this.data.selectItem
    this.cancleFollow(userId,index)
  },
  // 取消关注
  cancleFollow: async function(userId,index){
    let {list} = this.data
    const res = await request.post(api.cancleFollow+userId,{},{loading:false})
    console.log('取消关注',res);
    if(res.httpStatus==200){
      list[index].follow = false
      list[index].eachFollow = false
      this.setData({
        list
      })
      this.followModal.hide()
      wx.showToast({
        title: '取消成功',
        icon:'none'
      })
    }
  },
  // 关注
  handleFollow: async function(userId,index){
    let {list} = this.data
    const res = await request.post(api.follow + userId,{},{loading:false})
    console.log('关注',res);
    if(res.httpStatus==200){
      list[index].follow = true
      list[index].eachFollow = true
      this.setData({
        list
      })
      wx.showToast({
        title: '关注成功',
        icon:'none'
      })
    }
  },
  //跳转个人主页
  toHomePage:function(e){
    // console.log('e',e.currentTarget.dataset.userid);
    let id = e.currentTarget.dataset.userid
    wx.navigateTo({
      url: `/pages/home/home?id=${id}`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.followModal = this.selectComponent('#followModal')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let {isFans,userId} = this.data
    this.setData({
      list:[]
    })
    if(isFans){
      this.getFansList(userId)
    }else{
      this.getFollowList(userId)
    }
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {isFans,userId,page,pages} = this.data
    if(page<pages){
      page++
      this.setData({
        page
      })

      if(isFans){
        this.getFansList(userId)
      }else{
        this.getFollowList(userId)
      }
    }else{
      this.setData({
        showNodata:true
      })
    }
  },

  // 用户点击右上角分享
	onShareAppMessage(e) {
		return shareOptions('default', e)
	},
})